package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.service.PlanOrderService;
import com.uwlaser.service.dto.PlanOrderDto;
import com.uwlaser.service.dto.PlanOrderQueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "订单管理管理")
@RequestMapping("/api/planOrder")
public class PlanOrderController {

    private final PlanOrderService planOrderService;

    @GetMapping
    @Log("查询订单管理")
    @ApiOperation("查询订单管理")
    @PreAuthorize("@el.check('planOrder:list')")
    public ResponseEntity query(PlanOrderQueryParam query, Pageable pageable){
        return new ResponseEntity<>(planOrderService.queryAll(query,pageable),HttpStatus.OK);
    }

    @GetMapping("/list")
    @Log("查询订单管理")
    @ApiOperation("查询订单管理")
    @PreAuthorize("@el.check('planOrder:list')")
    public ResponseEntity list(PlanOrderQueryParam query){
        return new ResponseEntity<>(planOrderService.queryAll(query),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增订单管理")
    @ApiOperation("新增订单管理")
    @PreAuthorize("@el.check('planOrder:add')")
    public ResponseEntity create(@Validated @RequestBody PlanOrderDto resources){
        return new ResponseEntity<>(planOrderService.insert(resources),HttpStatus.CREATED);
    }

    @PostMapping("/generate")
    @Log("生成工单")
    @ApiOperation("生成工单")
    @PreAuthorize("@el.check('planOrder:add')")
    public ResponseEntity generate(@Validated @RequestBody PlanOrderDto resources){
        return new ResponseEntity<>(planOrderService.generate(resources),HttpStatus.CREATED);
    }



    @PutMapping
    @Log("修改订单管理")
    @ApiOperation("修改订单管理")
    @PreAuthorize("@el.check('planOrder:edit')")
    public ResponseEntity update(@Validated @RequestBody PlanOrderDto resources){
        planOrderService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除订单管理")
    @ApiOperation("删除订单管理")
    @PreAuthorize("@el.check('planOrder:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        planOrderService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出订单管理")
    @ApiOperation("导出订单管理")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('planOrder:list')")
    public void download(HttpServletResponse response, PlanOrderQueryParam query) throws IOException {
        planOrderService.download(planOrderService.queryAll(query), response);
    }*/

}
